package com.freez.spark.core.operation;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.util.Arrays;

/**
 * FREEDOM  2021 人生苦短，不妨一试
 * FileName: CreateRDD.java
 * Author: zcs
 * Date: 2021年-12月-07日 周二 17:58
 * Description: RDD的创建方式
 */
public class CreateRDD {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf();
        conf.setAppName("SparkCore-CreateRDD").setMaster("local");
        JavaSparkContext jsc = new JavaSparkContext(conf);
        String path = ".src/main/resources/data/core/demo/markov.txt";
        JavaRDD<String> javaRDD = jsc.textFile("path");
        JavaRDD<Integer> parallelize = jsc.parallelize(Arrays.asList(1, 2, 3, 4, 5));
        JavaPairRDD<Integer, String> parallelizePairs = jsc.parallelizePairs(Arrays.asList(
                new Tuple2<>(1, "张三"),
                new Tuple2<>(2, "Freedom"),
                new Tuple2<>(3, "zcs")
        ));
        javaRDD.foreach(s -> System.out.println(s));
        parallelize.foreach(s -> System.out.println(s));
        parallelizePairs.foreach(s -> System.out.println(s));
        jsc.close();
    }
}